package com.turing.post.cardmanagement.util;

import java.io.Serializable;

import com.turing.post.util.PCPSUtil;

/**
 * CPU卡应用目录下的交易明细文件(EF18)
 * 
 * @author yxb
 * 
 */
@SuppressWarnings("serial")
public class CardEPDetail implements Serializable{
	/**
	 * 01－02 电子钱包消费、圈存交易序号 2 HEX
	 */
	private int tranSeqNo;

	public void setTranSeqNo(int tranSeqNo) {
		this.tranSeqNo = tranSeqNo;
	}

	public int getTranSeqNo() {
		return tranSeqNo;
	}

	/**
	 * 03－05 透支限额 3 HEX
	 */
	private int overdraftLimit;

	public void setOverdraftLimit(int overdraftLimit) {
		this.overdraftLimit = overdraftLimit;
	}

	public int getOverdraftLimit() {
		return overdraftLimit;
	}

	/**
	 * 06－09 交易金额 4 HEX
	 */
	private int amount;

	public void setAmount(int amount) {
		this.amount = amount;
	}

	public int getAmount() {
		return amount;
	}

	/**
	 * ED圈存
	 */
	public static final String TRANTYPE_ED_LOAD = "01";

	/**
	 * EP圈存
	 */
	public static final String TRANTYPE_EP_LOAD = "02";

	/**
	 * 圈提
	 */
	public static final String TRANTYPE_UNLOAD = "03";

	/**
	 * ED取款
	 */
	public static final String TRANTYPE_ED_WITHDRAW = "04";

	/**
	 * ED消费
	 */
	public static final String TRANTYPE_ED_CONSUME = "05";

	/**
	 * EP消费
	 */
	public static final String TRANTYPE_EP_CONSUME = "06";

	/**
	 * ED修改透资限额
	 */
	public static final String TRANTYPE_ED_MODIFY_OVERDRAFTLIMIT = "07";

	/**
	 * 信用消费
	 */
	public static final String TRANTYPE_CREDIT_CONSUME = "08";

	/**
	 * 10－10 交易类型 1 BCD
	 */
	private String tranType;

	public void setTranType(String tranType) {
		this.tranType = tranType;
		if (PCPSUtil.compareObject(TRANTYPE_ED_LOAD, tranType))
			this.tranTypeDesc = "ED圈存";
		else if (PCPSUtil.compareObject(TRANTYPE_EP_LOAD, tranType))
			this.tranTypeDesc = "EP圈存";
		else if (PCPSUtil.compareObject(TRANTYPE_UNLOAD, tranType))
			this.tranTypeDesc = "圈提";
		else if (PCPSUtil.compareObject(TRANTYPE_ED_WITHDRAW, tranType))
			this.tranTypeDesc = "ED取款";
		else if (PCPSUtil.compareObject(TRANTYPE_ED_CONSUME, tranType))
			this.tranTypeDesc = "ED消费";
		else if (PCPSUtil.compareObject(TRANTYPE_EP_CONSUME, tranType))
			this.tranTypeDesc = "EP消费";
		else if (PCPSUtil.compareObject(TRANTYPE_ED_MODIFY_OVERDRAFTLIMIT,
				tranType))
			this.tranTypeDesc = "ED修改透资限额";
		else if (PCPSUtil.compareObject(TRANTYPE_CREDIT_CONSUME, tranType))
			this.tranTypeDesc = "信用消费";
		else
			this.tranTypeDesc = "未知[" + this.tranType + "]";
	}

	public String getTranType() {
		return tranType;
	}

	/**
	 * 交易类型描述
	 */
	private String tranTypeDesc;

	public String getTranTypeDesc() {
		return tranTypeDesc;
	}

	/**
	 * 11－16 交易终端编号 6 BCD
	 */
	private String terminalNo;

	public void setTerminalNo(String terminalNo) {
		this.terminalNo = terminalNo;
	}

	public String getTerminalNo() {
		return terminalNo;
	}

	/**
	 * 17－20 交易日期 4 YYYYMMDD
	 */
	private String tranDate;

	public void setTranDate(String tranDate) {
		this.tranDate = tranDate;
	}

	public String getTranDate() {
		return tranDate;
	}

	/**
	 * 21－23 交易时间 3 HHMMSS
	 */
	private String tranTime;

	public void setTranTime(String tranTime) {
		this.tranTime = tranTime;
	}

	public String getTranTime() {
		return tranTime;
	}

	/**
	 * 解析字节数组内容到钱包交易明细对象
	 * 
	 * @param detail
	 * @return
	 */
	public static CardEPDetail parseEPDetail(byte[] detail) {
		if (detail == null || detail.length != 0x17)
			return null;

		CardEPDetail edp = new CardEPDetail();

		int ival;

		// 解析tranSeqNo
		ival = PCPSUtil.parseNetworkInteger(detail, 0, 2);
		edp.setTranSeqNo(ival);

		// 解析overdraftLimit
		ival = PCPSUtil.parseNetworkInteger(detail, 2, 3);
		edp.setOverdraftLimit(ival);

		// 解析amount
		ival = PCPSUtil.parseNetworkInteger(detail, 5, 4);
		edp.setAmount(ival);

		// 解析tranType
		edp.setTranType(PCPSUtil.getByteHexString(detail[9]));

		// 转换为tranTypeDesc

		// 解析terminalNo
		edp.setTerminalNo(PCPSUtil.getHexString(detail, 10, 6));

		// 解析tranDate
		edp.setTranDate(PCPSUtil.getHexString(detail, 16, 4));

		// 解析tranTime
		edp.setTranTime(PCPSUtil.getHexString(detail, 20, 3));

		return edp;
	}
}
